package com.zbkj.service.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.constants.Constants;
import com.zbkj.common.constants.IntegralDetailConstants;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.user.*;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.*;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.common.vo.dateLimitUtilVo;
import com.zbkj.service.dao.UserIntegralDetailDao;
import com.zbkj.service.service.UserIntegralDetailService;
import com.zbkj.service.service.UserIntegralService;
import com.zbkj.service.service.UserService;
import com.zbkj.service.util.IntegralUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 用户积分记录Service实现类
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class UserIntegralDetailServiceImpl extends ServiceImpl<UserIntegralDetailDao, UserIntegralDetail> implements UserIntegralDetailService {

    private static final Logger logger = LoggerFactory.getLogger(UserIntegralDetailServiceImpl.class);

    @Resource
    private UserIntegralDetailDao dao;

    @Autowired
    private UserService userService;
    @Autowired
    private UserIntegralService userIntegralService;
    @Autowired
    private UserIntegralDetailService userIntegralDetailService;

    @Autowired
    private IntegralUtil integralUtil;



    @Autowired
    private TransactionTemplate transactionTemplate;

//    -------------------begin---------------------
    @Override
    public List<UserIntegralDetail> getList(FundsMonitorSearchRequest request, PageParamRequest pageParamRequest) {
        return null;
    }


    /**
     * 新增/消耗  总金额
     *
     * @param changeType       Integer 0 = 支出 1 = 获得
     * @param uid   Integer 用户uid
     * @param integralType String 类型
     * @param updateTime     String 时间范围
     * @return UserIntegralDetail
     */
    @Override
    public BigDecimal getAllBigDecimal(Integer uid, Integer changeType, String integralType, String updateTime) {
        QueryWrapper<UserIntegralDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("integral_type",integralType);
        if (ObjectUtil.isNotNull(uid)) {
            queryWrapper.eq("uid", uid);
        }
        if (null != changeType) {
            queryWrapper.eq("change_type", changeType);
        }
        if (null != updateTime) {
            dateLimitUtilVo dateLimit = DateUtil.getDateLimit(updateTime);
            queryWrapper.between("update_time", dateLimit.getStartTime(), dateLimit.getEndTime());
        }
        List<UserIntegralDetail> userIntegralDetails = dao.selectList(queryWrapper);
        if (CollUtil.isEmpty(userIntegralDetails)) {
            return BigDecimal.ZERO;
        }
        return userIntegralDetails.stream().map(UserIntegralDetail::getValue).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_DOWN);
    }
    /**
     * 保存退款日志
     * @return boolean
     */
    @Override
    public Boolean saveRefundDetail(StoreOrderRefundRequest request, UserIntegral userIntegral, String integralType) {
        UserIntegralDetail userIntegralDetail = new UserIntegralDetail();
        userIntegralDetail.setUid(userIntegral.getUid());
        userIntegralDetail.setIntegralType(integralType);
        userIntegralDetail.setValue(request.getAmount());
        userIntegralDetail.setChangeType(IntegralDetailConstants.CHANGE_TYPE_DEC);
        userIntegralDetail.setReason("订单退款" + request.getAmount() + integralType);
        userIntegralDetail.setRemainder(integralUtil.getIntegralByType(userIntegral, integralType).add(request.getAmount()));
        userIntegralDetail.setUpdateTime(DateUtil.nowDateTime());
        return userIntegralDetailService.save(userIntegralDetail);
    }

//    /**
//     * 资金监控
//     * @param request 查询参数
//     * @param pageParamRequest 分页参数
//     * @return PageInfo
//     */
//    @Override
//    public PageInfo<MonitorResponse> fundMonitoring(FundsMonitorRequest request, PageParamRequest pageParamRequest) {
//        Page<UserIntegralDetail> billPage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
//        Map<String, Object> map = new HashMap<>();
//        if (StrUtil.isNotBlank(request.getKeywords())) {
//            map.put("keywords", StrUtil.format("%{}%", request.getKeywords()));
//        }
//        //时间范围
//        if (StrUtil.isNotBlank(request.getDateLimit())) {
//            dateLimitUtilVo dateLimit = DateUtil.getDateLimit(request.getDateLimit());
//            map.put("startTime", dateLimit.getStartTime());
//            map.put("endTime", dateLimit.getEndTime());
//        }
//        // 明细类型筛选
//        if (StrUtil.isNotBlank(request.getTitle())) {
//            switch (request.getTitle()) {
//                case "recharge" :
//                    map.put("title", "充值支付");
//                    break;
//                case "admin" :
//                    map.put("title", "后台操作");
//                    break;
//                case "productRefund" :
//                    map.put("title", "商品退款");
//                    break;
//                case "payProduct" :
//                    map.put("title", "购买商品");
//                    break;
//            }
//
//        }
//        List<UserBillResponse> userBillResponses = dao.fundMonitoring(map);
//        if (CollUtil.isEmpty(userBillResponses)) {
//            return CommonPage.copyPageInfo(billPage, CollUtil.newArrayList());
//        }
//        List<MonitorResponse> responseList = userBillResponses.stream().map(e -> {
//            MonitorResponse monitorResponse = new MonitorResponse();
//            BeanUtils.copyProperties(e, monitorResponse);
//            return monitorResponse;
//        }).collect(Collectors.toList());
//        return CommonPage.copyPageInfo(billPage, responseList);
//    }

//    -------------------end---------------------------




    @Override
    public List<UserIntegralDetail> findListByOrderIdAndUid(String orderNo, Integer uid) {
        return null;
    }

    /*
    * 获取未核验的余额提现信息明细
    * status ：已核验， 待核验， 核验失败
    * */
    @Override
    public UserIntegralDetail getDetailByCondition( Integer uid, String integralType, Integer changeType, BigDecimal value, String status) {
        LambdaQueryWrapper<UserIntegralDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserIntegralDetail::getUid, uid);
        lqw.eq(UserIntegralDetail::getIntegralType, integralType);
        lqw.eq(UserIntegralDetail::getChangeType, changeType);
        lqw.eq(UserIntegralDetail::getValue, value);
        lqw.eq(UserIntegralDetail::getStatus, status);
        lqw.last(" limit 1");
        return dao.selectOne(lqw);
    }


    // 更新未核验的余额提现信息明细
    @Override
    public Boolean updateDetailByCondition(String hopeType, Integer uid, String integralType, Integer changeType, BigDecimal value, String status) {
        LambdaUpdateWrapper<UserIntegralDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(UserIntegralDetail::getStatus, hopeType)  // 设置要修改的字段名和新值
                .eq(UserIntegralDetail::getUid, uid)  // 添加等值条件
                .eq(UserIntegralDetail::getIntegralType, integralType)
                .eq(UserIntegralDetail::getChangeType, changeType)
                .eq(UserIntegralDetail::getValue, value)
                .eq(UserIntegralDetail::getStatus, status)
                .last(" limit 1");  // 添加限制条件，只更新一条记录
        // 执行更新操作
        return dao.update(null, updateWrapper) > 0;
    }


    @Override
    public PageInfo<UserIntegralDetailResponse> findAdminValueAddList(AdminIntegralSearchRequest request, PageParamRequest pageParamRequest) {
        Page<UserIntegralDetailResponse> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        if (ObjectUtil.isNotNull(request.getUid())) {
            lqw.eq(UserIntegralDetail::getUid, request.getUid());
        }
        if (ObjectUtil.isNotNull(request.getUid())) {
            lqw.eq(UserIntegralDetail::getUid, request.getUid());
        }

        if (StrUtil.isNotBlank(request.getKeywords())) {
            List<Integer> idList = userService.findIdListLikeName(request.getKeywords());
            if (CollUtil.isNotEmpty(idList)) {
                lqw.in(UserIntegralDetail::getUid, idList);
            } else {
                return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
            }
        }
        //时间范围
        if (StrUtil.isNotBlank(request.getDateLimit())) {
            dateLimitUtilVo dateLimit = DateUtil.getDateLimit(request.getDateLimit());
            //判断时间
            int compareDateResult = DateUtil.compareDate(dateLimit.getEndTime(), dateLimit.getStartTime(), Constants.DATE_FORMAT);
            if (compareDateResult == -1) {
                throw new CrmebException("开始时间不能大于结束时间！");
            }

            lqw.between(UserIntegralDetail::getUpdateTime, dateLimit.getStartTime(), dateLimit.getEndTime());
        }
        // 积分类型
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_VALUE_ADD);
        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        List<UserIntegralDetail> list = dao.selectList(lqw);
        if (CollUtil.isEmpty(list)) {
            return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
        }
        List<UserIntegralDetailResponse> responseList = list.stream().map(i -> {
            UserIntegralDetailResponse response = new UserIntegralDetailResponse();
            BeanUtils.copyProperties(i, response);
            return response;
        }).collect(Collectors.toList());
        return CommonPage.copyPageInfo(page, responseList);
    }

    @Override
    public PageInfo<UserIntegralDetailResponse> findAdminGreenList(AdminIntegralSearchRequest request, PageParamRequest pageParamRequest) {
        Page<UserIntegralDetailResponse> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        if (ObjectUtil.isNotNull(request.getUid())) {
            lqw.eq(UserIntegralDetail::getUid, request.getUid());
        }
        if (StrUtil.isNotBlank(request.getKeywords())) {
            List<Integer> idList = userService.findIdListLikeName(request.getKeywords());
            if (CollUtil.isNotEmpty(idList)) {
                lqw.in(UserIntegralDetail::getUid, idList);
            } else {
                return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
            }
        }
        //时间范围
        if (StrUtil.isNotBlank(request.getDateLimit())) {
            dateLimitUtilVo dateLimit = DateUtil.getDateLimit(request.getDateLimit());
            //判断时间
            int compareDateResult = DateUtil.compareDate(dateLimit.getEndTime(), dateLimit.getStartTime(), Constants.DATE_FORMAT);
            if (compareDateResult == -1) {
                throw new CrmebException("开始时间不能大于结束时间！");
            }

            lqw.between(UserIntegralDetail::getUpdateTime, dateLimit.getStartTime(), dateLimit.getEndTime());
        }
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_GREEN);

        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        List<UserIntegralDetail> list = dao.selectList(lqw);
        if (CollUtil.isEmpty(list)) {
            return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
        }
        List<UserIntegralDetailResponse> responseList = list.stream().map(i -> {
            UserIntegralDetailResponse response = new UserIntegralDetailResponse();
            BeanUtils.copyProperties(i, response);
            return response;
        }).collect(Collectors.toList());
        return CommonPage.copyPageInfo(page, responseList);
    }

    @Override
    public PageInfo<UserIntegralDetailResponse> findAdminContributionList(AdminIntegralSearchRequest request, PageParamRequest pageParamRequest) {
        Page<UserIntegralDetailResponse> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        if (ObjectUtil.isNotNull(request.getUid())) {
            lqw.eq(UserIntegralDetail::getUid, request.getUid());
        }
        if (StrUtil.isNotBlank(request.getKeywords())) {
            List<Integer> idList = userService.findIdListLikeName(request.getKeywords());
            if (CollUtil.isNotEmpty(idList)) {
                lqw.in(UserIntegralDetail::getUid, idList);
            } else {
                return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
            }
        }
        //时间范围
        if (StrUtil.isNotBlank(request.getDateLimit())) {
            dateLimitUtilVo dateLimit = DateUtil.getDateLimit(request.getDateLimit());
            //判断时间
            int compareDateResult = DateUtil.compareDate(dateLimit.getEndTime(), dateLimit.getStartTime(), Constants.DATE_FORMAT);
            if (compareDateResult == -1) {
                throw new CrmebException("开始时间不能大于结束时间！");
            }

            lqw.between(UserIntegralDetail::getUpdateTime, dateLimit.getStartTime(), dateLimit.getEndTime());
        }
        // 积分类型
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_CONTRIBUTION);

        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        List<UserIntegralDetail> list = dao.selectList(lqw);
        if (CollUtil.isEmpty(list)) {
            return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
        }
        List<UserIntegralDetailResponse> responseList = list.stream().map(i -> {
            UserIntegralDetailResponse response = new UserIntegralDetailResponse();
            BeanUtils.copyProperties(i, response);
            return response;
        }).collect(Collectors.toList());
        return CommonPage.copyPageInfo(page, responseList);
    }

    @Override
    public PageInfo<UserIntegralDetailResponse> findAdminBalanceList(AdminIntegralSearchRequest request, PageParamRequest pageParamRequest) {
        Page<UserIntegralDetailResponse> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        if (ObjectUtil.isNotNull(request.getUid())) {
            lqw.eq(UserIntegralDetail::getUid, request.getUid());
        }
        if (StrUtil.isNotBlank(request.getKeywords())) {
            List<Integer> idList = userService.findIdListLikeName(request.getKeywords());
            if (CollUtil.isNotEmpty(idList)) {
                lqw.in(UserIntegralDetail::getUid, idList);
            } else {
                return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
            }
        }
        //时间范围
        if (StrUtil.isNotBlank(request.getDateLimit())) {
            dateLimitUtilVo dateLimit = DateUtil.getDateLimit(request.getDateLimit());
            //判断时间
            int compareDateResult = DateUtil.compareDate(dateLimit.getEndTime(), dateLimit.getStartTime(), Constants.DATE_FORMAT);
            if (compareDateResult == -1) {
                throw new CrmebException("开始时间不能大于结束时间！");
            }

            lqw.between(UserIntegralDetail::getUpdateTime, dateLimit.getStartTime(), dateLimit.getEndTime());
        }
        // 积分类型
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_BALANCE);


        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        List<UserIntegralDetail> list = dao.selectList(lqw);
        if (CollUtil.isEmpty(list)) {
            return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
        }
        List<UserIntegralDetailResponse> responseList = list.stream().map(i -> {
            UserIntegralDetailResponse response = new UserIntegralDetailResponse();
            BeanUtils.copyProperties(i, response);
            return response;
        }).collect(Collectors.toList());
        return CommonPage.copyPageInfo(page, responseList);
    }

    @Override
    public PageInfo<UserIntegralDetailResponse> findAdminCbfundsList(AdminIntegralSearchRequest request, PageParamRequest pageParamRequest) {
        Page<UserIntegralDetailResponse> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        if (ObjectUtil.isNotNull(request.getUid())) {
            lqw.eq(UserIntegralDetail::getUid, request.getUid());
        }
        if (StrUtil.isNotBlank(request.getKeywords())) {
            List<Integer> idList = userService.findIdListLikeName(request.getKeywords());
            if (CollUtil.isNotEmpty(idList)) {
                lqw.in(UserIntegralDetail::getUid, idList);
            } else {
                return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
            }
        }
        //时间范围
        if (StrUtil.isNotBlank(request.getDateLimit())) {
            dateLimitUtilVo dateLimit = DateUtil.getDateLimit(request.getDateLimit());
            //判断时间
            int compareDateResult = DateUtil.compareDate(dateLimit.getEndTime(), dateLimit.getStartTime(), Constants.DATE_FORMAT);
            if (compareDateResult == -1) {
                throw new CrmebException("开始时间不能大于结束时间！");
            }

            lqw.between(UserIntegralDetail::getUpdateTime, dateLimit.getStartTime(), dateLimit.getEndTime());
        }
        // 积分类型
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_CB_FUNDS);

        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        List<UserIntegralDetail> list = dao.selectList(lqw);
        if (CollUtil.isEmpty(list)) {
            return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
        }
        List<UserIntegralDetailResponse> responseList = list.stream().map(i -> {
            UserIntegralDetailResponse response = new UserIntegralDetailResponse();
            BeanUtils.copyProperties(i, response);
            return response;
        }).collect(Collectors.toList());
        return CommonPage.copyPageInfo(page, responseList);
    }




    /**
     * 根据类型条件计算积分总数
     * @param uid 用户uid
     * @return 积分总数
     */


//    @Override
//    public BigDecimal getSumValueAddIntegral(Integer uid) { // 获取增值积分
//        QueryWrapper<UserIntegralDetail> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("sum(value_add_integral) as value_add_integral");
//        queryWrapper.eq("uid", uid);
//        UserIntegralDetail integralRecord = dao.selectOne(queryWrapper);
//        if (ObjectUtil.isNull(integralRecord) || ObjectUtil.isNull(integralRecord.getValueAddIntegral())) {
//            System.out.println("valueAdd 为0........");
//            return new BigDecimal(0);
//        }
//        return integralRecord.getValueAddIntegral();
//    }
//
//    public BigDecimal getSumGreenIntegral(Integer uid) { // 获取绿色积分
//        QueryWrapper<UserIntegral> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("sum(green_integral) as green_integral");
//        queryWrapper.eq("uid", uid);
//        UserIntegral integralRecord = dao.selectOne(queryWrapper);
//        if (ObjectUtil.isNull(integralRecord) || ObjectUtil.isNull(integralRecord.getGreenIntegral())) {
//            System.out.println("green 为0........");
//            return new BigDecimal(0);
//        }
//        System.out.println("green 不为0........");
//        return integralRecord.getGreenIntegral();
//    }
//
//    @Override
//    public BigDecimal getSumContributionIntegral(Integer uid) { // 获取贡献值
//        QueryWrapper<UserIntegral> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("sum(contribution_integral) as contribution_integral");
//        queryWrapper.eq("uid", uid);
//        UserIntegral integralRecord = dao.selectOne(queryWrapper);
//        if (ObjectUtil.isNull(integralRecord) || ObjectUtil.isNull(integralRecord.getContributionIntegral())) {
//            System.out.println("contribution 为0........");
//            return new BigDecimal(0);
//        }
//        return integralRecord.getContributionIntegral();
//    }
//
//    @Override
//    public BigDecimal getSumBalance(Integer uid) { // 获取余额
//        QueryWrapper<UserIntegral> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("sum(balance) as balance");
//        queryWrapper.eq("uid", uid);
//        UserIntegral integralRecord = dao.selectOne(queryWrapper);
//        if (ObjectUtil.isNull(integralRecord) || ObjectUtil.isNull(integralRecord.getBalance())) {
//            System.out.println("balance 为0........");
//            return new BigDecimal(0);
//        }
//        return integralRecord.getBalance();
//    }
//
//    @Override
//    public BigDecimal getSumCbfunds(Integer uid) { // 获取cb基金
//        QueryWrapper<UserIntegral> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("sum(cbfunds) as cbfunds");
//        queryWrapper.eq("uid", uid);
//        UserIntegral integralRecord = dao.selectOne(queryWrapper);
//        if (ObjectUtil.isNull(integralRecord) || ObjectUtil.isNull(integralRecord.getCbfunds())) {
//            System.out.println("cbfunds 为0........");
//
//            return new BigDecimal(0);
//        }
//        return integralRecord.getCbfunds();
//    }
//
    /**
     * H5用户积分列表
     * @param uid 用户uid
     * @param pageParamRequest 分页参数
     * @return 记录列表
     */
//   '积分类型：0：增值积分 ，1：绿色积分，2：贡献值， 3：余额 ， 4：cb基金',
    @Override
    public List<UserIntegralDetail> findUserValueAddIntegralList(Integer uid, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        lqw.eq(UserIntegralDetail::getUid, uid);
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_VALUE_ADD);
        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        return dao.selectList(lqw);
    }

    @Override
    public List<UserIntegralDetail> findUserGreenIntegralList(Integer uid, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        lqw.eq(UserIntegralDetail::getUid, uid);
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_GREEN);
        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        return dao.selectList(lqw);
    }
    @Override
    public List<UserIntegralDetail> findUserContributionIntegralList(Integer uid, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        lqw.eq(UserIntegralDetail::getUid, uid);
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_CONTRIBUTION);
        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        return dao.selectList(lqw);
    }

    @Override
    public List<UserIntegralDetail> findUserBalanceList(Integer uid, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        lqw.eq(UserIntegralDetail::getUid, uid);
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_BALANCE);
        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        return dao.selectList(lqw);
    }

    @Override
    public List<UserIntegralDetail> findUserCbfundsList(Integer uid, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserIntegralDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(UserIntegralDetail::getId,UserIntegralDetail::getUid,UserIntegralDetail::getIntegralType, UserIntegralDetail::getRemainder,
                UserIntegralDetail::getChangeType,UserIntegralDetail::getValue, UserIntegralDetail::getReason, UserIntegralDetail::getUpdateTime);
        lqw.eq(UserIntegralDetail::getUid, uid);
        lqw.eq(UserIntegralDetail::getIntegralType, IntegralDetailConstants.INTEGRAL_TYPE_CB_FUNDS);
        lqw.orderByDesc(UserIntegralDetail::getUpdateTime);
        return dao.selectList(lqw);
    }

}

